﻿using CPPEI.WebApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using SER = CPPEI.ETL.Domain.Services;
using ENT = CPPEI.ETL.Domain.Entities;
using MOD = CPPEI.ETL.Domain.Models;
using CPPEI.ETL.Domain;

namespace CPPEI.ETL.WebApi
{
    [CPPEI.WebApi.AuthGetAction("ETL", "GetUnmappedDataTableList", "1.0")]
    public class GetUnmappedDataTableListAction : BaseAction<GetUnmappedDataTableListParams, GetUnmappedDataTableListResult>
    {
        protected override ActionResult<GetUnmappedDataTableListResult> OnExecute(ActionContext context,
            SysParameters sysParameters,
            GetUnmappedDataTableListParams opParams)
        {
            PagedList<ENT.EDtTable> serResult = new SER.DtTableQueryService().GetUnmappedTables(opParams.Filter, opParams.Page * 15, 15);
            if (serResult.IsSuccess)
            {
                GetUnmappedDataTableListResult result = new GetUnmappedDataTableListResult();

                result.Count = serResult.Data.Count;
                result.Total = serResult.Total;
                result.Limit = serResult.Limit;
                result.Start = serResult.Start;
                result.Page = opParams.Page;

                for (int index = 0; index < serResult.Data.Count; index++)
                {
                    GetUnmappedDataTableListResult.Item item = new GetUnmappedDataTableListResult.Item();
                    item.Id = serResult.Data[index].Id;
                    item.Name = serResult.Data[index].Name;
                    item.IsRawDt = serResult.Data[index].IsRawDt;
                    item.CreatedTime = serResult.Data[index].CreatedTime;

                    result.Items.Add(item);
                }
                return new ActionResult<GetUnmappedDataTableListResult>(result);
            }
            else
            {
                return new ActionResult<GetUnmappedDataTableListResult>(serResult.IsSuccess, serResult.ErrorCode, serResult.Message, null);
            }
        }
    }

    public class GetUnmappedDataTableListParams
    {
        public string Filter { get; set; }
        public int Page { get; set; }
    }

    public class GetUnmappedDataTableListResult : PagedResult
    {
        public List<Item> Items { get; set; }

        public GetUnmappedDataTableListResult()
        {
            Items = new List<Item>();
        }

        public class Item
        {
            public Guid Id { get; set; }
            public string Name { get; set; }

            public DateTime CreatedTime { get; set; }

            public bool IsRawDt { get; set; }
        }
    }
}
